<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-header style="background: #fff;padding: 12px 12px;height:auto;">
        <div>
          <el-form :model="queryData" inline>
            <el-form-item>
              <el-input v-model="queryData.zoneName" placeholder="方案名称" :rows="1" style="width: 280px;" />
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryData.status" filterable clearable placeholder="状态">
                <el-option v-for="item in zoneStatusOptions" :key="item.code" :value="item.code" :label="item.name" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="list">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-header>
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;margin-bottom: 12px;display: flex;">
          <el-button type="primary" @click="handelAdd">新增</el-button>
        </div>
        <el-table :columns="table_header" :data="table_body" :border="true" :max-height="tableHeight">
          <el-table-column prop="name" label="分区方案名称" min-width="100" align="center" />
          <el-table-column label="状态" min-width="100" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.status == 0" style="color: red">未启用</span>
              <span v-else style="color: #4298ef">已启用</span>
            </template>
          </el-table-column>
          <el-table-column prop="createdBy" label="创建人" min-width="100" align="center" />
          <el-table-column prop="createdAt" label="创建时间" min-width="100" align="center" />
          <el-table-column prop="createdBy" label="操作人" min-width="100" align="center" />
          <el-table-column prop="editedAt" label="最后更新时间" min-width="100" align="center" />
          <el-table-column label="操作" min-width="100" align="center">
            <template slot-scope="scope">
              <template v-if="scope.row.status == 0">
                <el-button type="text" @click="edit(scope.row.id)">编辑</el-button>
                <el-button type="text" @click="enable(scope.row)">启用</el-button>
              </template>
              <el-button type="text" @click="edit(scope.row.id)">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" @size-change="onPageSizeChange" @current-change="onPageChange" />
        </div>
      </el-footer>
    </el-container>
    <New ref="newZone" @query="list" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import request from '@/utils/request'
import New from './zone_new.vue'

export default {
  components: {
    New
  },
  data() {
    return {
      pageTotal: 0,
      pageSizeOpts: [50, 100],
      table_body: [],
      table_header: [
        {
          key: 'name',
          title: '分区方案名称',
          minWidth: 100,
          align: 'center'
        },
        {
          slot: 'status',
          title: '状态',
          minWidth: 100,
          align: 'center'
        },
        {
          key: 'createdBy',
          title: '创建人',
          minWidth: 100,
          align: 'center'
        },
        {
          key: 'createdAt',
          title: '创建时间',
          minWidth: 100,
          align: 'center'
        },
        {
          key: 'createdBy',
          title: '操作人',
          minWidth: 100,
          align: 'center'
        },
        {
          key: 'editedAt',
          title: '最后更新时间',
          minWidth: 100,
          align: 'center'
        },
        {
          slot: 'operation',
          title: '操作',
          minWidth: 100,
          align: 'center'
        }
      ],
      queryData: {
        pageSize: 50,
        currentPage: 1,
        zoneName: '',
        editor: '',
        region: '',
        status: ''
      },
      zoneStatusOptions: [
        { code: '0', name: '未启用' },
        { code: '1', name: '已启用' }
      ]
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 300
    },
    pageSize() {
      return this.pageSizeOpts[0]
    },
    ...mapState(//
      {
        customerOptions: state => state.base.customerOptions,
        channelOptions: state => state.base.channelOptions
      }
    )
  },
  created() {
    this.list()
  },
  methods: {
     list() {
      request({
        url: `/zone/query`,
        method: 'post',
        data: this.queryData
      }).then(res => {
        if (res.success) {
          this.pageTotal = res.data.total
          this.table_body = res.data.records
        }
      })
    },
    enable(row) {
      const _this = this
      this.$confirm('分区方案启用后不可再编辑,确认启用吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        request({
          url: `/zone/enable?id=` + row.id,
          method: 'get'
        }).then(res => {
          if (res.success) {
            _this.list()
          }
        })
      })
    },
    handelAdd() {
      this.$refs.newZone.show()
    },
    edit(id) {
      const newZone = this.$refs.newZone
      request({
        url: `/zone/detail?id=` + id,
        method: 'get'
      }).then(res => {
        if (res.success) {
          newZone.edit(res.data)
        }
      })
    },
    onPageSizeChange(pageSize) {
      this.queryData.pageSize = pageSize
      this.list()
    },
    onPageChange(page) {
      this.queryData.currentPage = page
      this.queryData.resetCurrentPage = false
      this.list()
    }
  }
}
</script>
